package vn.gq.tym.scr;

import vn.gq.tym.comictemplate.R;
import vn.gq.tym.widget.OnViewPagerDisablePagingListener;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.polites.android.GestureImageView;
import com.polites.android.GestureImageViewListener;
import com.polites.android.ZoomInOutListener;
import com.polites.android.ZoomStateListener;

public class ImageViewScr extends Fragment implements ZoomStateListener, OnClickListener {
	
	public static ImageViewScr newInstance(String url) {
		ImageViewScr f = new ImageViewScr();
		Bundle args = new Bundle();
		args.putString("URL", url);
		f.setArguments(args);
		return f;
	}
	
	private DisplayImageOptions options;
	private GestureImageView mImageView;
	private ProgressBar spinner;
	private Button reloadBtn;
	private ImageLoader imageLoader = ImageLoader.getInstance();
	private GestureDetector mGestureDetector;
	private SimpleOnGestureListener gestureListener = new SimpleOnGestureListener();
	private Button zoomIn, zoomOut;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		options = new DisplayImageOptions.Builder()
			//.showImageOnFail(R.drawable.ic_404notfound)
			.resetViewBeforeLoading()
			.cacheOnDisc()
			.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
			.bitmapConfig(Bitmap.Config.RGB_565)
			.displayer(new FadeInBitmapDisplayer(300))
			.build();
		mGestureDetector = new GestureDetector(getActivity(), gestureListener);
		mGestureDetector.setOnDoubleTapListener((OnDoubleTapListener) getActivity());
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		View convertView = inflater.inflate(R.layout.imageview_fragment, container, false);
		mImageView = (GestureImageView) convertView.findViewById(R.id.photoview);
		mImageView.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				mGestureDetector.onTouchEvent(event);
				return false;
			}
		});
		
		mImageView.setZoomStateListener(this);
		spinner = (ProgressBar) convertView.findViewById(R.id.loading);
		reloadBtn = (Button) convertView.findViewById(R.id.retry);
		reloadBtn.setOnClickListener(this);
		zoomIn = (Button) convertView.findViewById(R.id.button_zoom_in);
		zoomIn.setOnClickListener(this);
		zoomOut = (Button) convertView.findViewById(R.id.button_zoom_out);
		zoomOut.setOnClickListener(this);
		
		mImageView.setZoomInOutListener(new ZoomInOutListener() {
			
			@Override
			public void onDisableZoomOut() {
				getActivity().runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						zoomOut.setEnabled(false);
					}
				});
			}
			
			@Override
			public void onDisableZoomIn() {
				getActivity().runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						zoomIn.setEnabled(false);
					}
				});
			}

			@Override
			public void onZoom() {
				getActivity().runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						if (!zoomOut.isEnabled()) zoomOut.setEnabled(true);
						if (!zoomIn.isEnabled()) zoomIn.setEnabled(true);
					}
				});
			}
		});
		
		loadImage();
		return convertView;
	}
	
	public void loadImage() {
		imageLoader.displayImage(getArguments().getString("URL"), mImageView, options, new SimpleImageLoadingListener() {

			@Override
			public void onLoadingStarted(String imageUri, View view) {
				spinner.setVisibility(View.VISIBLE);
			}

			@Override
			public void onLoadingFailed(String imageUri, View view,
					FailReason failReason) {
				spinner.setVisibility(View.GONE);
				reloadBtn.setVisibility(View.VISIBLE);
				Log.e("longhoanggiang", "---------error: "+String.valueOf(getArguments().getString("URL")));
			}

			@Override
			public void onLoadingComplete(String imageUri, View view,
					Bitmap loadedImage) {
				spinner.setVisibility(View.GONE);
			}

			@Override
			public void onLoadingCancelled(String imageUri, View view) {
				super.onLoadingCancelled(imageUri, view);
			}
			
		});
	}

	public void onFailed() {
		
	}
	
	@Override
	public void onZoom(boolean isZoom) {
		if (getActivity() instanceof OnViewPagerDisablePagingListener) {
			((OnViewPagerDisablePagingListener) getActivity()).onDisable(isZoom);
		}
	}

	@Override
	public void onClick(View v) {
		if (v.equals(reloadBtn)) {
			reloadBtn.setVisibility(View.GONE);
			spinner.setVisibility(View.VISIBLE);
			loadImage();
		} else if(v.equals(zoomIn)) {
			mImageView.setZoomInOut(true);
		} else if (v.equals(zoomOut)) {
			mImageView.setZoomInOut(false);
		}
	}

	
}
